package com.itheima.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class CookiesUtils {
    public static Map<String, String> getCookie(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        String token = "";
        String accessToken = "";
        String expiresIn = "";
        try {
            Cookie[] cookies = request.getCookies();
            for (Cookie cookie : cookies) {
                switch (cookie.getName()) {
                    case "accessToken":
                        accessToken = cookie.getValue();
                        break;
                    case "expiresIn":
                        expiresIn = cookie.getValue();
                        break;
                    case "token":
                        token = cookie.getValue();
                        break;
                    default:
                        break;
                }
            }
            map.put("token", token);
            map.put("accessToken", accessToken);
            map.put("expiresIn", expiresIn);
        } catch (Exception e) {
            System.out.println("从cookie获取到相应的值失败----" + e.getMessage());
        }
        return map;
    }



    public static String getCookie(HttpServletRequest request ,String key) {
        Cookie[] cookies = request.getCookies();
        String value = null;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(key)) {
                value = cookie.getValue();
            }
        }
        return value;
    }



}
